#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
class Solution {
public:
    vector<vector<int>> fourSum(vector<int>& nums, int target) {

        sort(nums.begin(), nums.end());
        vector<vector<int>> vt;
        int a, b, c, d;
        for (a = 0; a < nums.size() - 3; a++)
        {
            if (nums.size() < 4)
            {
                break;
            }
            for (b = a + 1; b < nums.size() - 2; b++)
            {

                c = b + 1;
                d = nums.size() - 1;
                while (c < d)
                {
                    long max = nums[c] + nums[d];

                    if (nums[a] + nums[b] == target - max)
                    {

                        vt.push_back({ nums[a],nums[b],nums[c],nums[d] });
                        while (c < d && nums[c] == nums[c + 1]) {
                            c++;
                        }
                        while (c < d && nums[d] == nums[d - 1]) {
                            d--;
                        }
                        c++;
                        d--;
                    }
                    else if (nums[a] + nums[b] < target - max)
                    {
                        c++;
                    }
                    else {
                        d--;
                    }
                }
                while (b < nums.size() - 2 && nums[b] == nums[b + 1])
                {
                    b++;
                }
            }
            while (a < nums.size() - 3 && nums[a] == nums[a + 1])
            {
                a++;
            }
        }
        return vt;
    }
};

int main()
{
    Solution su;

    vector<int> v1 = { 1,0,-1,0,-2,2 };
    su.fourSum(v1, 0);

    return 0;
}